package com.itcast.demo_one.kotlin

fun main() {
    // demo01()

    demo02()
}

fun demo01() {
    val numbers = listOf(1, 2, 3, 4, 5)

    // 过滤 - 找出偶数
    val evens = numbers.filter { it % 2 == 0 }

    // 映射 - 每个元素加倍
    val doubled = numbers.map { it * 2 }  // [2, 4, 6, 8, 10]

    // 查找 - 找到第一个大于3的元素
    val firstLarge = numbers.first { it > 3 }  // 4

    println("$evens, $doubled, $firstLarge")

    // 遍历 - 打印元素
    numbers.forEach { println("Number: $it") }
}


fun demo02() {
    fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
        return operation(a, b)
    }

    val res1 = calculate(2, 3) { a, b -> a + b }
    println("加: $res1")

    val res2 = calculate(2, 3) { a, b -> a - b }
    println("减: $res2")
}

// 快速格式化代码: ctrl + alt + L